package com.huarui.saleReturn.action;

import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;
import org.ewaf.framework.action.BaseAction;

import com.huarui.saleReturn.button.SaleReturnButtonService;
import com.huarui.saleReturn.model.SaleReturn;
import com.huarui.saleReturn.service.SaleReturnService;
import com.huarui.saleReturn.util.ConstantParamEnum;
import com.huarui.user.model.SessionMessage;
import com.huarui.user.security.UserHolder;

/**
 * 销售退货处理
 * 
 * @author jiwei.yi
 * 
 */
public class SaleReturnAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = -3453471779776007177L;
	private Logger log = Logger.getLogger(SaleReturnAction.class);
	private SessionMessage sessionMessage;
	private Map<String,Object> resultMap;
	private String queryParam;
	private SaleReturnButtonService saleReturnButtonService;
	private SaleReturnService saleReturnService;
	/**
	 * 返回新建页面按钮
	 * @return  ['isOwner':true/false,'buttons':xxxxx]
	 */
	public String getInitialButtons(){
		sessionMessage = UserHolder.getCurrentSessionMessage();
		resultMap = saleReturnButtonService.generateButtonsByStatus(SaleReturn.STATUS_NEW,sessionMessage.getUserId() , sessionMessage.getUserId());
		return SUCCESS;
	}
	/**
	 * 由浏览页面跳转至编辑页面
	 * @return
	 */
	public String getSaleReturnAndItemById(){
		resultMap = new HashMap<String, Object>();
		try {
			resultMap = saleReturnService.getSaleReturnAndItemById(Integer.valueOf(queryParam));
			resultMap.put(ConstantParamEnum.SUCCESS.getType(), true);
			resultMap.put(ConstantParamEnum.MSG.getType(), "");
		} catch (NumberFormatException e) {
			log.error(ConstantParamEnum.OPEN_FAILURE.getType(), e);
			resultMap.put(ConstantParamEnum.SUCCESS.getType(), false);
			resultMap.put(ConstantParamEnum.MSG.getType(), ConstantParamEnum.OPEN_FAILURE.getType());
		}
		return SUCCESS;
	}
	
	/**
	 * 删除草稿状态下的销售退货单
	 * @return
	 */
	public String deleteCreate(){
		resultMap = new HashMap<String, Object>();
		try {
			resultMap = saleReturnService.deleteSaleReturnById(Integer.valueOf(queryParam));
		} catch (Exception e) {
			log.error(ConstantParamEnum.DELETE_DRAFT_FAILURE.getType(), e);
			resultMap.put(ConstantParamEnum.SUCCESS.getType(), false);
			resultMap.put(ConstantParamEnum.MSG.getType(), ConstantParamEnum.DELETE_DRAFT_FAILURE.getType());
		}
		return SUCCESS;
	}
	/**
	 *待仓库确认状态退回草稿状态 
	 * @return
	 */
	public String backToCreate(){
		resultMap = new HashMap<String, Object>();
		try {
			resultMap = saleReturnService.updateSaleReturnStatusById(Integer.valueOf(queryParam));
		} catch (Exception e) {
			log.error(ConstantParamEnum.BACK_TO_DRAFT_FAILURE.getType(), e);
			resultMap.put(ConstantParamEnum.SUCCESS.getType(), false);
			resultMap.put(ConstantParamEnum.MSG.getType(), ConstantParamEnum.BACK_TO_DRAFT_FAILURE.getType());
		}
		return SUCCESS;
	}
	/**
	 * 保存草稿
	 * @return
	 */
	public String backupDraft(){
		resultMap = new HashMap<String, Object>();
		try {
			resultMap = saleReturnService.backupCreate(queryParam);
			resultMap.put(ConstantParamEnum.SUCCESS.getType(), true);
			resultMap.put(ConstantParamEnum.MSG.getType(), ConstantParamEnum.SAVE_CREATE_SUCCESS.getType());
		} catch (Exception e) {
			log.error(ConstantParamEnum.SAVE_DRAFT_FAILURE.getType(),e);
			resultMap.put(ConstantParamEnum.SUCCESS.getType(), false);
			resultMap.put(ConstantParamEnum.MSG.getType(), ConstantParamEnum.SAVE_DRAFT_FAILURE.getType());
		}
		return SUCCESS;
	}
	/**
	 * 提交仓库
	 * @return
	 */
	public String backupSubmit(){
		resultMap = new HashMap<String, Object>();
		try {
			resultMap = saleReturnService.backupSubmit(queryParam);
			resultMap.put(ConstantParamEnum.SUCCESS.getType(), true);
			resultMap.put(ConstantParamEnum.MSG.getType(), ConstantParamEnum.SAVE_SUBMIT_SUCCESS.getType());
		} catch (Exception e) {
			log.error(ConstantParamEnum.SAVE_SUBMIT_FAILURE.getType(),e);
			resultMap.put(ConstantParamEnum.SUCCESS.getType(), false);
			resultMap.put(ConstantParamEnum.MSG.getType(), ConstantParamEnum.SAVE_SUBMIT_FAILURE.getType());
		}
		return SUCCESS;
	}
	/**
	 * 入库
	 * @return
	 */
	public String delivery(){
		resultMap = new HashMap<String, Object>();
		try {
			resultMap = saleReturnService.backupDelivery(queryParam);
			resultMap.put(ConstantParamEnum.SUCCESS.getType(), true);
			resultMap.put(ConstantParamEnum.MSG.getType(), ConstantParamEnum.SAVE_DELIVERY_SUCCESS.getType());
		} catch (Exception e) {
			log.error(ConstantParamEnum.SAVE_DELIVERY_FAILURE.getType(),e);
			resultMap.put(ConstantParamEnum.SUCCESS.getType(), false);
			resultMap.put(ConstantParamEnum.MSG.getType(), ConstantParamEnum.SAVE_DELIVERY_FAILURE.getType());
		}
		return SUCCESS;
	}
	/**
	 * 接手单据
	 * @return
	 */
	public String acceptSaleReturn(){
		resultMap = new HashMap<String, Object>();
		SessionMessage sm = UserHolder.getCurrentSessionMessage();
		try {
			resultMap = saleReturnService.updateSaleReturnCurrentorId(Integer.valueOf(queryParam),sm.getUserId());
		} catch (Exception e) {
			log.debug(ConstantParamEnum.ACCEPT_RECEIPT_FAILURE.getType(), e);
			resultMap.put(ConstantParamEnum.SUCCESS.getType(), false);
			resultMap.put(ConstantParamEnum.MSG.getType(), ConstantParamEnum.ACCEPT_RECEIPT_FAILURE.getType());
		}
		return SUCCESS;
	}
	
	/**
	 * 释放单据
	 * @return
	 */
	public String releaseSaleReturn(){
		resultMap = new HashMap<String, Object>();		
		try {
			resultMap = saleReturnService.updateSaleReturnCurrentorId(Integer.valueOf(queryParam),null);
		} catch (Exception e) {
			log.debug(ConstantParamEnum.RELEASE_RECEIPT_FAILURE.getType(), e);
			resultMap.put(ConstantParamEnum.SUCCESS.getType(), false);
			resultMap.put(ConstantParamEnum.MSG.getType(), ConstantParamEnum.RELEASE_RECEIPT_FAILURE.getType());
		}
		return SUCCESS;
	}
	
	public void setSaleReturnButtonService(SaleReturnButtonService saleReturnButtonService) {
		this.saleReturnButtonService = saleReturnButtonService;
	}

	public Map<String, Object> getResultMap() {
		return resultMap;
	}

	public void setResultMap(Map<String, Object> resultMap) {
		this.resultMap = resultMap;
	}

	public void setQueryParam(String queryParam) {
		this.queryParam = queryParam;
	}

	public void setSaleReturnService(SaleReturnService saleReturnService) {
		this.saleReturnService = saleReturnService;
	}

}
